package com.lkd.handler;

import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.OrderCheck;
import com.lkd.emq.Topic;
import com.lkd.entity.OrderEntity;
import com.lkd.service.OrderService;
import com.lkd.utils.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.common.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Topic(TopicConfig.ORDER_CHECK_TOPIC)
@Slf4j
public class OrderCheckHandler implements MsgHandler {

    @Autowired
    private OrderService orderService;

    @Override
    public void process(String jsonMsg) throws IOException {
        //解析参数
        OrderCheck orderCheck = JsonUtil.getByJson(jsonMsg, OrderCheck.class);
        if(orderCheck==null|| Strings.isNullOrEmpty(orderCheck.getOrderNo())){
            return;
        }
        //查询订单
        OrderEntity order = orderService.getByOrderNo(orderCheck.getOrderNo());
        if(order==null){
            return;
        }
        if(order.getStatus().equals(VMSystem.ORDER_STATUS_CREATE)){
            order.setStatus(VMSystem.ORDER_STATUS_INVALID);
            orderService.updateById(order);
            log.info("订单超时，已取消，订单号{}"+order.getOrderNo());
        }
    }
}
